package com.example.user.customviewdemo.drawing.text;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * 前言：但行好事，莫问前程。只需努力每一天。
 * Created by user on 2016/4/25.
 * 详解android  drawText（）
 */
public class MyTextView extends View{
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);

        Paint paint = new Paint();
        paint.setStrokeWidth(2);
        paint.setTextSize(120);
        paint.setColor(Color.RED);
        String text = "fghijklmnIOPQ";

        /**********************画出文字的基线，ascent，descent，top，bottom线***********************************/
        /**
         * 使用总结：通过FontMetric fm = paint.getFontMetric()获取到FontMetric；
         *          ascent线Y坐标 = baseline线的y坐标 + fontMetric.ascent；
                    descent线Y坐标 = baseline线的y坐标 + fontMetric.descent；
                     top线Y坐标 = baseline线的y坐标 + fontMetric.top；
                     bottom线Y坐标 = baseline线的y坐标 + fontMetric.bottom；
         */
        int baseLineX = 50;
        int baseLineY = 500;

        Paint.FontMetrics metrics = paint.getFontMetrics();
        float ascent = baseLineY + metrics.ascent;
        float descent = baseLineY + metrics.descent;
        float top = baseLineY + metrics.top;
        float bottom = baseLineY + metrics.bottom;

        canvas.drawLine(baseLineX,baseLineY,1200,baseLineY,paint);
        canvas.drawLine(baseLineX,top,1200,top,paint);
        canvas.drawLine(baseLineX,ascent,1200,ascent,paint);
        canvas.drawLine(baseLineX,descent,1200,descent,paint);
        canvas.drawLine(baseLineX,bottom,1200,bottom,paint);
        paint.setStrokeWidth(10);
        canvas.drawPoint(baseLineX,baseLineY,paint);

        /******************所绘制文本的宽度、高度、和最小矩形的获取********************************************/
        /**
         * 使用总结：
         * 1、所绘制文本的高度为bottom线的Y坐标 - top线的Y坐标：
                                                  Paint.FontMetrics fm = paint.getFontMetrics();
                                                  int top = baseLineY + fm.top;
                                                  int bottom = baseLineY + fm.bottom;
                                                  //所占高度
                                                  int height = bottom - top = fm.bottom - fm.top;
         * 2、所绘制文本的宽度：int width = paint.measureText(String text);
         * 3、获取最小矩形是通过paint.getTextBounds（）函数获取的，但是该函数获取到的最小矩形的基线坐标原点是（0,0）
         *    而ascent的y坐标为负数，所以只需将坐标点向下平移到baseLineY即可，代码如下：
                                                   Rect minRect = new Rect();
                                                   paint.getTextBounds(text,0,text.length(),minRect);
                                                   //最小矩形，实际top位置
                                                   int minTop = bounds.top + baselineY;
                                                   //最小矩形，实际bottom位置
                                                  int minBottom = bounds.bottom + baselineY;
         */
        //画出最大区域
        float width = paint.measureText(text);
        paint.setColor(Color.GREEN);
        canvas.drawRect(baseLineX,top,baseLineX + width,bottom,paint);
        //画出最小区域
        paint.setColor(Color.RED);
        Rect rect = new Rect();
        paint.getTextBounds(text,0,text.length(),rect);
        float minTop = rect.top + baseLineY;
        float minBottom = rect.bottom + baseLineY;
        canvas.drawRect(baseLineX,minTop,baseLineX + width,minBottom,paint);
        paint.setColor(Color.BLACK);
        canvas.drawText(text,baseLineX,baseLineY,paint);



    }

}
